package com.sinosoft.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.sinosoft.base.BaseFormController;
import com.sinosoft.base.Constants;
import com.sinosoft.product.DataQueryManager;
import com.sinosoft.product.DataQueryModel;
import com.sinosoft.product.DataQueryModelSet;
import com.sinosoft.product.ZywxImageAll;
import com.sinosoft.shopcar.ShopcarManager;
import com.sinosoft.util.SystemConfigUtil;
import com.sinosoft.util.SystemLogUtil;

@Controller
public class DataSearchColltroller extends BaseFormController {
	private DataQueryManager mgr = null;

	@Autowired
	public void setMgr(DataQueryManager mgr) {
		this.mgr = mgr;
	}

	private ShopcarManager shopcar = null;

	@Autowired
	public void setShopcar(ShopcarManager shopcar) {
		this.shopcar = shopcar;
	}

	private MessageSource messageSource = null;

	@Autowired
	public void setMessageSource(MessageSource messageSource) {
		this.messageSource = messageSource;
	}

	@SuppressWarnings("unchecked")
	@RequestMapping("/productsearch.html")
	public ModelAndView getQueryPage(HttpServletRequest request,
			HttpServletResponse reponse, Model model) {
		/*
		 * hubo
		 */
		String lang4img = null;
		if(request.getSession(false)==null){
			lang4img = "zh_CN";
		}else{
			lang4img = request.getSession().getAttribute("lang").toString();
		}
		HashMap sateandsen = new HashMap();
		//satellite
		String[] str = SystemConfigUtil.getSystemValue("satelliteforcollect",
				getServletContext());
		for (int i = 0; i < str.length; i++) {
			String[] str1 = SystemConfigUtil.getSystemValue(str[i],
					getServletContext());
			sateandsen.put(str[i], str1);
		}
		model.addAttribute("satellite", sateandsen);
		// model.addAttribute("datamodel", datamodel);

		ArrayList<DataQueryModel> nationalList = new ArrayList();// 页面上显示国家
		String sql = " select t.gid,t.name ,t.en_name from apsco_geo_world t where t.name is not null";
		nationalList = mgr.getNational(sql);
		model.addAttribute("nationalList", nationalList);

		MessageSourceAccessor text = new MessageSourceAccessor(messageSource,
				request.getLocale());
		String systemlanguage = text.getMessage("system.language");
		model.addAttribute("systemlanguage", systemlanguage);

		// 查询所有的数据
		// String pages = mgr.getAllPageSize();
		// double size = Constants.pagesize;
		// int allpages = (int) Math.ceil(Integer.valueOf(pages)
		// / size);
		model.addAttribute("pagesizes", "0");
		model.addAttribute("allpage", "0");
		model.addAttribute("language4img",lang4img);
		return new ModelAndView("product/productSearch", "", null);
	}

	// 显示所有的数据到地图上
	@RequestMapping("/allDataSearch.html")
	public String dataQuery(DataQueryModel datamodel,
			HttpServletRequest request, Model model) {
		String ltlong = request.getParameter("leftTlong");
		String lTlat = request.getParameter("leftTlat");
		String rBlong = request.getParameter("rightBlong");
		String rBlat = request.getParameter("rightBlat");
		String productSelect = request.getParameter("prosele");

		String imagegsd = request.getParameter("imagegsd");
		String scenedatestart = request.getParameter("scenedatestart");
		String scenedateend = request.getParameter("scenedateend");
		String cloudpercent = request.getParameter("cloudpercent");
		String overalldataquality = request.getParameter("overalldataquality");
		String sateAndsensor = request.getParameter("satellites");
		String national = request.getParameter("national");
		String province = request.getParameter("province");
		String county = request.getParameter("county");

		DataQueryModel datamod = new DataQueryModel();
		datamod = DataQueryModelSet.getQueryModel(ltlong, rBlong, lTlat, rBlat,
				productSelect, null, imagegsd, scenedatestart, scenedateend,
				cloudpercent, overalldataquality, sateAndsensor, national,
				province, county);

		List<ZywxImageAll> datalist = mgr.getAllDataQuery(datamod);
		// XML格式的数据传输
		StringBuffer products = new StringBuffer();
		StringBuffer satellites = new StringBuffer();
		StringBuffer sensors = new StringBuffer();

		// 左上角
		StringBuffer lefttlong = new StringBuffer();
		StringBuffer lefttlat = new StringBuffer();
		// 右上角
		StringBuffer righttlong = new StringBuffer();
		StringBuffer righttlat = new StringBuffer();
		// 右下角
		StringBuffer rightblong = new StringBuffer();
		StringBuffer rightblat = new StringBuffer();
		// 左下角
		StringBuffer leftblong = new StringBuffer();
		StringBuffer leftblat = new StringBuffer();

		for (int i = 0; i < datalist.size(); i++) {
			ZywxImageAll zywximage0 = datalist.get(i);
			if (datalist.size() == (i + 1)) {
				products.append(zywximage0.getProductid().toString());
				satellites.append(zywximage0.getSatelliteid());
				sensors.append(zywximage0.getSensorid());

				lefttlong.append(zywximage0.getTopleftlongitude());
				lefttlat.append(zywximage0.getTopleftlatitude());

				righttlong.append(zywximage0.getToprightlongitude());
				righttlat.append(zywximage0.getToprightlatitude());

				rightblong.append(zywximage0.getBottomrightlongitude());
				rightblat.append(zywximage0.getBottomrightlatitude());

				leftblong.append(zywximage0.getBottomleftlongitude());
				leftblat.append(zywximage0.getBottomleftlatitude());

			} else {
				products.append(zywximage0.getProductid().toString() + ",");
				satellites.append(zywximage0.getSatelliteid() + ",");
				sensors.append(zywximage0.getSensorid() + ",");

				lefttlong.append(zywximage0.getTopleftlongitude() + ",");
				lefttlat.append(zywximage0.getTopleftlatitude() + ",");

				righttlong.append(zywximage0.getToprightlongitude() + ",");
				righttlat.append(zywximage0.getToprightlatitude() + ",");

				rightblong.append(zywximage0.getBottomrightlongitude() + ",");
				rightblat.append(zywximage0.getBottomrightlatitude() + ",");

				leftblong.append(zywximage0.getBottomleftlongitude() + ",");
				leftblat.append(zywximage0.getBottomleftlatitude() + ",");
			}
		}
		request.setAttribute("porducts", products.toString());
		request.setAttribute("satellites", satellites.toString());
		request.setAttribute("sensors", sensors.toString());

		request.setAttribute("lefttlong", lefttlong.toString());
		request.setAttribute("lefttlat", lefttlat.toString());

		request.setAttribute("righttlong", righttlong.toString());
		request.setAttribute("righttlat", righttlat.toString());

		request.setAttribute("rightblong", rightblong.toString());
		request.setAttribute("rightblat", rightblat.toString());

		request.setAttribute("leftblong", leftblong.toString());
		request.setAttribute("leftblat", leftblat.toString());

		return "product/productXml";
	}

	// 显示10条数据到地图上
	@RequestMapping("/imagedataquery.html")
	public String ajax(ModelMap map, HttpServletResponse response,
			HttpServletRequest request) {
		String page = request.getParameter("getpage");
		String ltlong = request.getParameter("leftTlong");
		String lTlat = request.getParameter("leftTlat");
		String rBlong = request.getParameter("rightBlong");
		String rBlat = request.getParameter("rightBlat");
		String productSelect = request.getParameter("prosele");

		String imagegsd = request.getParameter("imagegsd");
		String scenedatestart = request.getParameter("scenedatestart");
		String scenedateend = request.getParameter("scenedateend");
		String cloudpercent = request.getParameter("cloudpercent");
		String overalldataquality = request.getParameter("overalldataquality");
		String sateAndsensor = request.getParameter("satellites");

		String national = request.getParameter("national");
		String province = request.getParameter("province");
		String county = request.getParameter("county");

		DataQueryModel datamod = new DataQueryModel();
		datamod = DataQueryModelSet.getQueryModel(ltlong, rBlong, lTlat, rBlat,
				productSelect, page, imagegsd, scenedatestart, scenedateend,
				cloudpercent, overalldataquality, sateAndsensor, national,
				province, county);

		List<ZywxImageAll> datalist = mgr.getDataQuery(datamod);

		String allProduct = mgr.getAllProductSize(datamod);
		double size = Constants.pagesize;
		int allPages = (int) Math.ceil(Integer.valueOf(allProduct) / size);

		// XML格式的数据传输
		StringBuffer products = new StringBuffer();
		StringBuffer satellites = new StringBuffer();
		StringBuffer sensors = new StringBuffer();

		StringBuffer rightblat = new StringBuffer();
		StringBuffer lefttlong = new StringBuffer();
		StringBuffer lefttlat = new StringBuffer();
		StringBuffer rightblong = new StringBuffer();

		// 右上角
		StringBuffer righttlong = new StringBuffer();
		StringBuffer righttlat = new StringBuffer();
		// 左下角
		StringBuffer leftblong = new StringBuffer();
		StringBuffer leftblat = new StringBuffer();
		// 接收站，景日期，中心经纬度
		StringBuffer receives = new StringBuffer();
		StringBuffer scenedates = new StringBuffer();
		StringBuffer centerlats = new StringBuffer();
		StringBuffer centerlongs = new StringBuffer();
		StringBuffer browsefilelocation = new StringBuffer();

		for (int i = 0; i < datalist.size(); i++) {
			ZywxImageAll zywximage0 = datalist.get(i);
			if (datalist.size() == (i + 1)) {// 将所有数据拼成list,最后一个没有逗号
				products.append(zywximage0.getProductid().toString());
				satellites.append(zywximage0.getSatelliteid());
				sensors.append(zywximage0.getSensorid());

				receives.append(zywximage0.getReceive());
				scenedates.append(zywximage0.getScenedate());
				centerlats.append(zywximage0.getCenterlatitude());
				centerlongs.append(zywximage0.getCenterlongitude());

				rightblat.append(zywximage0.getBottomrightlatitude());
				lefttlong.append(zywximage0.getTopleftlongitude());
				lefttlat.append(zywximage0.getTopleftlatitude());
				rightblong.append(zywximage0.getBottomrightlongitude());

				righttlong.append(zywximage0.getToprightlongitude());
				righttlat.append(zywximage0.getToprightlatitude());

				leftblong.append(zywximage0.getBottomleftlongitude());
				leftblat.append(zywximage0.getBottomleftlatitude());
				browsefilelocation.append(zywximage0.getBrowsefilelocation());

			} else {// 将所有数据拼成list
				products.append(zywximage0.getProductid().toString() + ",");
				satellites.append(zywximage0.getSatelliteid() + ",");
				sensors.append(zywximage0.getSensorid() + ",");

				receives.append(zywximage0.getReceive() + ",");
				scenedates.append(zywximage0.getScenedate() + ",");
				centerlats.append(zywximage0.getCenterlatitude() + ",");
				centerlongs.append(zywximage0.getCenterlongitude() + ",");

				rightblat.append(zywximage0.getBottomrightlatitude() + ",");
				lefttlong.append(zywximage0.getTopleftlongitude() + ",");
				lefttlat.append(zywximage0.getTopleftlatitude() + ",");
				rightblong.append(zywximage0.getBottomrightlongitude() + ",");

				righttlong.append(zywximage0.getToprightlongitude() + ",");
				righttlat.append(zywximage0.getToprightlatitude() + ",");

				leftblong.append(zywximage0.getBottomleftlongitude() + ",");
				leftblat.append(zywximage0.getBottomleftlatitude() + ",");
				browsefilelocation.append(zywximage0.getBrowsefilelocation()
						+ ",");
			}
		}
		request.setAttribute("page", allProduct + "," + allPages);
		request.setAttribute("porducts", products.toString());
		request.setAttribute("satellites", satellites.toString());
		request.setAttribute("sensors", sensors.toString());

		request.setAttribute("scenedates", scenedates.toString());
		request.setAttribute("receives", receives.toString());
		request.setAttribute("centerlats", centerlats.toString());
		request.setAttribute("centerlongs", centerlongs.toString());

		request.setAttribute("rightblat", rightblat.toString());
		request.setAttribute("lefttlong", lefttlong.toString());
		request.setAttribute("lefttlat", lefttlat.toString());
		request.setAttribute("rightblong", rightblong.toString());

		request.setAttribute("righttlong", righttlong.toString());
		request.setAttribute("righttlat", righttlat.toString());

		request.setAttribute("leftblong", leftblong.toString());
		request.setAttribute("leftblat", leftblat.toString());
		request.setAttribute("browsefilelocation", browsefilelocation.toString());
		
		return "product/productXml";

	}

	// 高亮显示
	@RequestMapping("/heigthLight.html")
	public String heigthLight(ModelMap map, HttpServletResponse response,
			HttpServletRequest request) {
		MessageSourceAccessor text = new MessageSourceAccessor(messageSource,
				request.getLocale());
		JSONObject jsonObj = new JSONObject();
		StringBuffer longlat = new StringBuffer();
		String uniqueness = request.getParameter("uniqueness");// 产品ID_卫星ID_传感器ID
		String level = request.getParameter("prosele");
		DataQueryModel datamod = new DataQueryModel();
		ZywxImageAll image = mgr.getDetails(datamod, level, uniqueness);

		// rblat, ltlong, ltlat, rblong, str, rtlong, rtlat,
		// lblong, lbla

		longlat.append(image.getBottomrightlatitude() + ",");
		longlat.append(image.getTopleftlongitude() + ",");
		longlat.append(image.getTopleftlatitude() + ",");
		longlat.append(image.getBottomrightlongitude() + ",");
		longlat.append(image.getSatelliteid() + "_" + image.getSensorid() + "_"
				+ image.getProductid() + ",");
		longlat.append(image.getToprightlongitude() + ",");
		longlat.append(image.getToprightlatitude() + ",");
		longlat.append(image.getBottomleftlongitude() + ",");
		longlat.append(image.getBottomleftlatitude());

		jsonObj.put("longlat", longlat.toString());

		try {
			response.getWriter().write(jsonObj.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			SystemLogUtil.saveError(Constants.LOG_ERROR, "heigthLight fail!");
			this.saveError(request, text
					.getMessage("heigthLight set json is fail"));
		}
		return null;
	}

	// 获取详细信息
	@RequestMapping("/getdatadetails.html")
	public String getDetails(ModelMap map, HttpServletResponse response,
			HttpServletRequest request) {
		String uniqueness = request.getParameter("uniqueness");// 产品ID_卫星ID_传感器ID
		String level = request.getParameter("prosele");

		DataQueryModel datamod = new DataQueryModel();
		ZywxImageAll image = mgr.getDetails(datamod, level, uniqueness);

		request.setAttribute("productid", image.getProductid());
		request.setAttribute("sceneid", image.getSceneid());
		request.setAttribute("productdate", image.getProductdate());
		request.setAttribute("mapprojection", image.getMapprojection());
		request.setAttribute("producttype", image.getProducttype());
		request.setAttribute("earthmodel", image.getEarthmodel());
		request.setAttribute("sensorid", image.getSensorid());
		request.setAttribute("satelliteid", image.getSatelliteid());
		request.setAttribute("receivestationid", image.getReceive());
		request.setAttribute("receivetime", image.getReceivetime());
		request.setAttribute("orbittype", image.getOrbittype());
		request.setAttribute("atttype", image.getAtttype());
		request.setAttribute("stripid", image.getStripid());
		request.setAttribute("producetype", image.getProducetype());
		request.setAttribute("productlevel", image.getProductlevel());
		request.setAttribute("productformat", image.getProductformat());
		request.setAttribute("scenedate", image.getScenedate());
		request.setAttribute("bandnumber", image.getBandnumber());
		request.setAttribute("scenepath", image.getScenepath());
		request.setAttribute("scenerow", image.getScenerow());
		request.setAttribute("satpath", image.getSatpath());
		request.setAttribute("satrow", image.getSatrow());
		request.setAttribute("scenecount", image.getScenecount());
		request.setAttribute("sceneshift", image.getSceneshift());
		request.setAttribute("starttime", image.getStarttime());
		request.setAttribute("endtime", image.getEndtime());
		request.setAttribute("centertime", image.getCentertime());
		request.setAttribute("startline", image.getStartline());
		request.setAttribute("endline", image.getEndline());
		request.setAttribute("imagegsd", image.getImagegsd());
		request.setAttribute("widthinpixels", image.getWidthinpixels());
		request.setAttribute("heightinpixels", image.getHeightinpixels());
		request.setAttribute("widthinmeters", image.getWidthinmeters());
		request.setAttribute("heightinmeters", image.getHeightinmeters());
		request.setAttribute("regionname", image.getRegionname());
		request.setAttribute("cloudpercent", image.getCloudpercent());
		request.setAttribute("datasize", image.getDatasize());
		request.setAttribute("rollviewingangle", image.getRollviewingangle());
		request.setAttribute("pitchviewingangle", image.getPitchviewingangle());
		request.setAttribute("rollsatelliteangle", image
				.getRollsatelliteangle());
		request.setAttribute("pitchsatelliteangle", image
				.getPitchsatelliteangle());
		request.setAttribute("yawsatelliteangle", image.getYawsatelliteangle());
		request.setAttribute("solarazimuth", image.getSolarazimuth());
		request.setAttribute("solarzenith", image.getSolarzenith());
		request.setAttribute("satelliteazimuth", image.getSatelliteazimuth());
		request.setAttribute("satellitezenith", image.getSatellitezenith());
		request.setAttribute("gainmode", image.getGainmode());
		request.setAttribute("integrationtime", image.getIntegrationtime());
		request.setAttribute("integrationlevel", image.getIntegrationlevel());
		request.setAttribute("earthellipsoid", image.getEarthellipsoid());
		request.setAttribute("zoneno", image.getZoneno());
		request.setAttribute("resamplingkernel", image.getResamplingkernel());
		request.setAttribute("heightmode", image.getHeightmode());
		request.setAttribute("ephemerisdata", image.getEphemerisdata());
		request.setAttribute("attitudedata", image.getAttitudedata());
		request.setAttribute("radiometricmethod", image.getRadiometricmethod());
		request.setAttribute("mtfcorrection", image.getMtfcorrection());
		request.setAttribute("denoise", image.getDenoise());
		request.setAttribute("rayleighcorrection", image
				.getRayleighcorrection());
		request.setAttribute("usedgcpno", image.getUsedgcpno());
		request.setAttribute("centerlatitude", image.getCenterlatitude());
		request.setAttribute("centerlongitude", image.getCenterlongitude());
		request.setAttribute("topleftlatitude", image.getTopleftlatitude());
		request.setAttribute("topleftlongitude", image.getTopleftlongitude());
		request.setAttribute("toprightlatitude", image.getToprightlatitude());
		request.setAttribute("toprightlongitude", image.getToprightlongitude());
		request.setAttribute("bottomrightlatitude", image
				.getBottomrightlatitude());
		request.setAttribute("bottomrightlongitude", image
				.getBottomrightlongitude());
		request.setAttribute("bottomleftlatitude", image
				.getBottomleftlatitude());
		request.setAttribute("bottomleftlongitude", image
				.getBottomleftlongitude());
		request.setAttribute("topleftmapx", image.getTopleftmapx());
		request.setAttribute("topleftmapy", image.getTopleftmapy());
		request.setAttribute("toprightmapx", image.getToprightmapx());
		request.setAttribute("toprightmapy", image.getToprightmapy());
		request.setAttribute("bottomrightmapx", image.getBottomrightmapx());
		request.setAttribute("bottomrightmapy", image.getBottomrightmapy());
		request.setAttribute("bottomleftmapx", image.getBottomleftmapx());
		request.setAttribute("bottomleftmapy", image.getBottomleftmapy());
		request.setAttribute("orbitid", image.getOrbitid());
		request.setAttribute("overalldataquality", image
				.getOveralldataquality());
		request.setAttribute("thumbfilelocation", image.getThumbfilelocation());

		return "product/productDetailXml";
	}

	// 提交到订单
	@RequestMapping("/submitToOrder.html")
	public String sunbmitToOrder(ModelMap map, HttpServletResponse response,
			HttpServletRequest request) {
		MessageSourceAccessor text = new MessageSourceAccessor(messageSource,
				request.getLocale());

		String uniqueness = request.getParameter("imgInfo");// 产品ID_卫星ID_传感器ID

		String level = request.getParameter("prosele");
		// System.out.println(level);
		if (level.equalsIgnoreCase("0")) {
			level = "LEVEL0PRO";
		} else {
			level = "LEVEL" + level;
		}

		/**
		 * 添加产品到购物车
		 * 
		 * @param level
		 *            产品级别
		 * @param keys
		 *            ([satellite_sensorid_productid,
		 *            satellite1_sensorid1_productid1])
		 * @return
		 */

		JSONObject jsonObj = new JSONObject();
		try {
			String username = SecurityUtils.getSubject().getPrincipal()
					.toString();
			jsonObj.put("dataBack", "1");
			// 添加到购物车
			shopcar.saveShopcarListByProductInfo(level, uniqueness);
		} catch (Exception e) {
			jsonObj.put("dataBack", "0");
			SystemLogUtil.saveError(Constants.LOG_ERROR,
					"pruductDisplay to shopcar fail!");
			this.saveError(request, text.getMessage("addToShopCar is fail"));
		}

		try {
			response.getWriter().write(jsonObj.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			SystemLogUtil.saveError(Constants.LOG_ERROR,
					"pruductDisplay to shopcar fail!");
			this.saveError(request, text.getMessage("set json is fail"));
		}

		return null;
	}

	// 取出本用户购物车已有的订单
	@RequestMapping("/getProFromShopcar.html")
	public String getProFromShopcar(ModelMap map, HttpServletResponse response,
			HttpServletRequest request) {
		JSONObject jsonObj = new JSONObject();
		MessageSourceAccessor text = new MessageSourceAccessor(messageSource,
				request.getLocale());
		try {
			String username = SecurityUtils.getSubject().getPrincipal()
					.toString();
			jsonObj.put("backData", mgr.getProFromCar(username));

		} catch (Exception e) {
			jsonObj.put("backData", "0");
		}

		try {
			response.getWriter().write(jsonObj.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			SystemLogUtil.saveError(Constants.LOG_ERROR,
					"getProFromShopcar fail!");
			this.saveError(request, text.getMessage("set json is fail"));
		}
		// mgr.getProFromCar(username)
		return null;

	}

	// 选国家出来省
	@SuppressWarnings("unchecked")
	@RequestMapping("/getprovince.html")
	public ModelAndView getprovince(String national,
			HttpServletRequest request, HttpServletResponse response) {
		national = "world".equalsIgnoreCase(national) ? "01111111" : national;
		MessageSourceAccessor text = new MessageSourceAccessor(messageSource,
				request.getLocale());
		String systemlanguage = text.getMessage("system.language");

		JSONObject jsonObj = new JSONObject();
		ArrayList<DataQueryModel> nationalList = new ArrayList();
		String sql = " select t.gid,t.name,t.en_name  from apsco_geo_states t where t.incountry = "
				+ "  (select t.en_name from apsco_geo_world t where t.gid = '"
				+ national + "') and t.name is not null  ";
		nationalList = mgr.getNational(sql);

		Map<String, String> province = new HashMap<String, String>();

		for (int i = 0; i < nationalList.size(); i++) {
			DataQueryModel datamodel = new DataQueryModel();
			datamodel = nationalList.get(i);
			if ("ZH_CN".equalsIgnoreCase(systemlanguage)) {
				province.put(datamodel.getProvinceid(), datamodel
						.getProvincename());
			}
			if ("EN".equalsIgnoreCase(systemlanguage)) {
				province.put(datamodel.getProvinceid(), datamodel
						.getProvinceenname());
			}

		}
		// 返回一个JSONArray对象

		jsonObj.put("province", province);

		try {
			response.getWriter().write(jsonObj.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;
	}

	// 选省出来县
	@RequestMapping("/getcounty.html")
	public ModelAndView getcounty(String province, HttpServletRequest request,
			HttpServletResponse response) {
		province = "province".equalsIgnoreCase(province) ? "01111111"
				: province;
		MessageSourceAccessor text = new MessageSourceAccessor(messageSource,
				request.getLocale());
		String systemlanguage = text.getMessage("system.language");

		JSONObject jsonObj = new JSONObject();
		ArrayList<DataQueryModel> nationalList = new ArrayList();
		String sql = "select t.gid, t.name, t.en_name, t.en_inprovince from apsco_geo_town t "
				+ " where t.en_inprovince = (select en_name from apsco_geo_states  where name is not null "
				+ " and gid = '" + province + "')  and t.name is not null  ";
		nationalList = mgr.getNational(sql);

		Map<String, String> county = new HashMap<String, String>();

		for (int i = 0; i < nationalList.size(); i++) {
			DataQueryModel datamodel = new DataQueryModel();
			datamodel = nationalList.get(i);
			if ("ZH_CN".equalsIgnoreCase(systemlanguage)) {
				county.put(datamodel.getCountyid(), datamodel.getCountyname());
			}
			if ("EN".equalsIgnoreCase(systemlanguage)) {
				county
						.put(datamodel.getCountyid(), datamodel
								.getCountyenname());
			}

		}
		// 返回一个JSONArray对象

		jsonObj.put("county", county);

		try {
			response.getWriter().write(jsonObj.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;
	}

	// 通过gid，获取一个行政区的polygon
	@RequestMapping("/adminpolygon.html")
	public ModelAndView adminpolygon(String gid, String admin,
			HttpServletRequest request, HttpServletResponse response) {
		String str = "";
		str = mgr.getPolygon(gid, admin);
		String state = str.substring(0, 4);
		if ("POLY".equalsIgnoreCase(state)) {
			str = str.replaceAll("POLYGON ", "").replaceAll("\\(\\(", "")
					.replaceAll("\\)\\)", "").replaceAll(", ", ",").replaceAll(
							" ", "_");
		}
		if ("MULT".equalsIgnoreCase(state)) {
			str = str.replaceAll("MULTIPOLYGON \\(\\(\\(", "").replaceAll(
					"\\)\\)\\)", "").replaceAll("\\)\\), \\(\\(", "&");
			str = str.replaceAll(", ", ",").replaceAll(" ", "_");
		}

		JSONObject jsonObj = new JSONObject();

		jsonObj.put("adminpolygon", str);
		jsonObj.put("state", state);

		try {
			response.getWriter().write(jsonObj.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;
	}

}
